<template>
	<view></view>
</template>

<script>
	import { myRequest } from '../../utils'
	
	export default {
		data() {
			return {
				scene: ''
			}
		},
		
		onLoad(options) {
			const that = this
			
			if (options.scene) {
				that.scene = decodeURIComponent(options.scene)
				uni.setStorageSync('scene', that.scene)
			}
			
			uni.showLoading({
			    title: '正在登录',
				success() {
					
					uni.getSetting({
						success(settingRes) {
							if (settingRes.authSetting['scope.userInfo']) {
								
								uni.getUserInfo({
									provider: 'weixin',
									success(userInfoRes) {
										
										uni.login({
											provider: 'weixin',
											success: function(loginRes) {
													
												let successFunc = function(res) {
													uni.hideLoading()
													uni.setStorageSync('sessionid', res.data.data.sessionId)
													
													if (res.data.code == 1001) {
														if (that.scene.length > 0) {
															uni.redirectTo({
															  url: '/pages/student_register/student_register'
															})
														} else {
															uni.redirectTo({
															  url: '/pages/expert_register/expert_register'
															})
														}
														return
													}
													
													if (res.data.code != 20000) {
														uni.showModal({
															title: '提示',
															content: res.data.message
														})
														return
													}
													
													let userInfo = res.data.data.userInfo
													uni.setStorageSync('userInfo', res.data.data.userInfo)
													
													if (userInfo.userType == 1) { // 学生
														uni.redirectTo({
														  url: '/pages/index/index'
														})
													} else if (userInfo.userType == 2) { // 专家
														uni.redirectTo({
														  url: '/pages/expert_fans/expert_fans'
														})
													}
												}
												
												let param = {
													url: '/no-login/mini',
													data: {
														code: loginRes.code,
														avatar: userInfoRes.userInfo.avatarUrl,
														nickName: userInfoRes.userInfo.nickName,
														gender: userInfoRes.userInfo.gender
													}
												}
												
												myRequest(param, successFunc)
												
											}
										})
										
									},
									fail() {
										uni.showModal({
											title: '提示',
											content: '登录失败，无法获取您的微信昵称和头像',
											showCancel: false
										})
									}
								})
								
							} else {
								uni.navigateTo({
								  url: '/pages/auth/auth'
								})
							}
						}
					})
					
				}
			})
		},
	}
</script>

<style>
	
</style>
